package de.baumann.browser.activitys.user

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.view.View
import de.baumann.browser.OdinGlideModule
import de.baumann.browser.R
import de.baumann.browser.activitys.BaseFileUploadActivity
import de.baumann.browser.activitys.MainActivity
import de.baumann.browser.iview.IUserInfoView
import de.baumann.browser.present.UserInfoPresenter
import de.baumann.browser.utils.Constants
import de.baumann.browser.utils.getPoolName
import de.baumann.browser.utils.getUser
import de.baumann.browser.utils.removeUser
import de.baumann.browser.views.dialog.ExitDialog
import kotlinx.android.synthetic.main.activity_user_info_setting.*
import kotlinx.android.synthetic.main.include_tool_bar.*

/**
 * @author：
 * @date：2018/10/22
 * @discription: 个人信息
 */
class UserInfoActivity : BaseFileUploadActivity<IUserInfoView, UserInfoPresenter>(), View.OnClickListener, IUserInfoView {

    private lateinit var exitDialog: ExitDialog

    private var headUrl: String = ""

    private var token: String = ""

    private var reUploadNum = 0

    override fun getTitleText(): String {
        return getString(R.string.personal_information)
    }

    override fun getLayout(): Int {
        return R.layout.activity_user_info_setting
    }

    override fun initView() {
        initToolbar(commToolBar, commTitle)
        viewEditNickname.setOnClickListener(this)
        viewEditQQ.setOnClickListener(this)
        viewEditETHUrl.setOnClickListener(this)
        viewEditPhone.setOnClickListener(this)
        ivUserInfoSettingHead.setOnClickListener(this)
        btnLogout.setOnClickListener(this)
    }

    override fun initData() {

    }

    override fun createPresenter(): UserInfoPresenter {
        return UserInfoPresenter()
    }

    override fun createView(): IUserInfoView {
        return this
    }

    override fun onResume() {
        super.onResume()
        presenter.getUserInfo()
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.btnLogout -> {
                exitDialog = ExitDialog(mContext)
                exitDialog.setOnExitClickListener(this)
                exitDialog.show()
            }

            R.id.viewEditNickname -> {
                val intent = Intent(mContext, EditUserInfoActivity::class.java)
                intent.putExtra(Constants.EDIT_TYPE, Constants.EDIT_TYPE_NICKNAME)
                intent.putExtra(Constants.NICK_NAME, nickname)
                startActivityForResult(intent, EDIT_NICKNAME_CODE)
            }
            R.id.viewEditQQ -> {
                val intentQQ = Intent(mContext, EditUserInfoActivity::class.java)
                intentQQ.putExtra(Constants.EDIT_TYPE, Constants.EDIT_TYPE_QQ)
                intentQQ.putExtra(Constants.QQ_CODE, qq)
                startActivityForResult(intentQQ, EDIT_QQ_CODE)
            }
            R.id.viewEditETHUrl -> BindETHActivity.startBindETHActivity(this@UserInfoActivity, EDIT_ETH_CODE, ethUrl)
            R.id.tvExit -> {
                exitDialog.dismiss()
                presenter.logout()
            }
            R.id.viewEditPhone -> BindPhoneActivity.startSettingPhoneActivity(this@UserInfoActivity,
                    tvPhone.text.trim().toString(), EDIT_PHONE_CODE)
            R.id.ivUserInfoSettingHead -> showModeSelectDialog()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (requestCode == TAKEPICTURE || requestCode == ALBUMDELECT) {
            super.onActivityResult(requestCode, resultCode, data)
            return
        }
        if (resultCode != Activity.RESULT_OK || null == data) {
            return
        }

        when (requestCode) {
            EDIT_NICKNAME_CODE -> tvNickname.text = data.getStringExtra(Constants.NICK_NAME)
            EDIT_QQ_CODE -> tvQQcode.text = data.getStringExtra(Constants.QQ_CODE)
            EDIT_ETH_CODE -> tvETHUrl.text = data.getStringExtra(Constants.ETH_URL)
            EDIT_PHONE_CODE -> tvPhone.text = getUser()!!.phone
        }
    }

    override fun setPoolName(poolName: String?) {
        tvEUPoolName.text = getPoolName()
    }


    override fun setNickname(nickname: String?) {
        tvNickname.text = nickname
    }

    override fun setQQ(qq: String?) {
        tvQQcode.text = qq
    }

    override fun setEthUrl(ethUrl: String?) {
        tvETHUrl.text = ethUrl
    }

    override fun setPhone(phone: String?) {
        tvPhone!!.text = phone
    }

    override fun getNickname(): String {
        return tvNickname.text.trim().toString()
    }

    override fun getQQ(): String {
        return tvQQcode.text.trim().toString()
    }

    override fun getEthUrl(): String {
        return tvETHUrl.text.trim().toString()
    }

    override fun logoutSuccess() {
        removeUser()
        startActivity(Intent(mContext, MainActivity::class.java))
    }

    override fun setToken(token: String) {
        this.token = token
        uploadToQiNiu(headUrl, token)
    }

    override fun setHeadhUrl(headUrl: String) {
        this.headUrl = headUrl
        OdinGlideModule.loadWithCropAndCircle(mContext, headUrl, ivUserInfoSettingHead, R.drawable.icon_head)
    }

    override fun setBitmap(bitmapPath: String) {
        setHeadhUrl(bitmapPath)
        presenter.getQiNiuToken()
    }

    override fun uploadSuccess(url: String) {
        presenter.editHead(url)
    }

    override fun uploadFailed(path: String) {
        if (reUploadNum >= 3) {
            showShortToast(getString(R.string.avatar_upload_failed))
            return
        }
        reUploadNum++
        uploadToQiNiu(headUrl, token)
    }

    companion object {
        private const val EDIT_NICKNAME_CODE = 1010
        private const val EDIT_QQ_CODE = 1011
        private const val EDIT_ETH_CODE = 1012
        private const val EDIT_PHONE_CODE = 1013

        fun startUserInfoSettingActivity(context: Context) {
            context.startActivity(Intent(context, UserInfoActivity::class.java))
        }
    }
}
